<template>
  <div class="home">
    <!-- 头部 -->
    <div class="tou">
      <div class="l" @click="$router.go(-1)">返回</div>
      <div class="c">
        <van-search v-model="value" placeholder="请输入搜索关键词" />
      </div>
      <div class="r" @click="sou">搜索</div>
    </div>
    <!-- 内容区 -->
    <div class="all" v-if="allList.length > 0">
      <div
        class="list"
        v-for="(item, index) in allList"
        :key="index"
        @click="goxqing(item.id)"
      >
        <div class="t1">{{ item.course_classify_title }}</div>
        <div class="t2">{{ item.title }}</div>
        <div class="t3">
          <van-tag
            round
            type="default"
            size="medium"
            v-for="(ite, ind) in item.course_label"
            :key="ind"
            style="margin-right: 10px"
            >{{ ite.label_name }}</van-tag
          >
        </div>
        <div class="t4">
          <div
            class="tu"
            v-for="(ite, ind) in item.teacher.slice(0, 3)"
            :key="ind"
          >
            <img :src="ite.avatar" alt="" class="img" />
            <div class="zi" style="margin-top: 10px">
              {{ ite.teacher_name }}
            </div>
          </div>
        </div>
        <div class="t5">
          <div class="dis">
            <div class="l">{{ item.sales_num }}人已报名</div>
            <div class="r" style="color: green" v-if="item.price == 0">
              免费
            </div>
            <div class="r" style="color: red" v-else>￥{{ item.price }}</div>
          </div>
        </div>
      </div>
    </div>
    <!-- 空状态 -->
    <van-empty description="暂无该数据哦!" v-else />
  </div>
</template>

<script>
import { toRefs, reactive, onMounted } from 'vue'
import { Toast } from 'vant'
import { getSearchApi } from '../http/api.js'
import { useRoute, useRouter } from 'vue-router'
export default {
  setup(props) {
    const route = useRoute()
    const router = useRouter()
    const data = reactive({
      value: '',
      allList: []
    })
    onMounted(async () => {
      console.log('传过来的数值', route.query.title)
      //   请求接口
      let obj = {
        title: route.query.title,
        classify_id: 1
      }
      const res = await getSearchApi(obj)
      console.log('搜索的接口', res)
      data.allList = res
    })
    // 渲染页面
    const sou = async () => {
      //   请求接口
      let obj = {
        title: data.value,
        classify_id: 1
      }
      const res = await getSearchApi(obj)
      console.log('搜索的接口2', res)
      data.allList = res
    }
    // 点击跳转到详情页面
    const goxqing = (id) => {
      router.push(`/xq?basis_id=${id}`)
    }
    return {
      ...toRefs(data),
      sou,
      goxqing
    }
  }
}
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 5px 20px;
  padding-bottom: 90px;
  .tou {
    width: 100%;
    height: 100px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #eee;
    .l,
    .r {
      width: 10%;
    }
    .c {
      width: 72%;
      height: 60px;
      margin-top: -55px;
    }
  }
  .all {
    width: 100%;
    box-sizing: border-box;
    padding: 20px 0;
    .list {
      width: 100%;
      box-sizing: border-box;
      margin-bottom: 18px;
      padding-bottom: 20px;
      padding-top: 8px;
      box-shadow: 0 0 2px #ccc;
      .t1 {
        width: 18%;
        background-color: orangered;
        color: #fff;
        padding: 2px 15px;
        text-align: center;
        font-size: 12px;
        margin-left: 5px;
        border-top-left-radius: 20px;
        border-bottom-right-radius: 20px;
      }
      .t2,
      .t3,
      .t4,
      .t5 {
        margin-top: 25px;
        padding: 0 15px;
      }
      .t4 {
        width: 100%;
        box-sizing: border-box;
        display: flex;
        .tu {
          width: 33.3%;
          display: flex;
          .img {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            margin-right: 25px;
          }
        }
      }
      .t5 {
        width: 100%;
        box-sizing: border-box;
        .dis {
          width: 100%;
          box-sizing: border-box;
          display: flex;
          justify-content: space-between;
        }
      }
    }
  }
}
</style>
